package com.imgo.max.temporary.service;

import com.imgo.max.common.dto.ResultCodeEnum;
import com.imgo.max.common.dto.ResultDO;
import com.imgo.max.common.util.PhotoUtil;
import com.imgo.max.temporary.dao.SuperGirlConcertDao;
import com.imgo.max.temporary.dto.StarDto;
import com.imgo.max.temporary.dto.SuperGirlConcertActivityDto;
import com.imgo.max.temporary.dto.SuperGirlConcertDetailDto;
import com.imgo.max.temporary.dto.SuperGirlConcertDto;
import com.imgo.max.user.dto.User;
import com.imgo.max.user.service.UserCacheService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by loumingshan on 16/5/12.
 * 超女线下活动接口
 */
@Service("superGirlConcertService")
public class SuperGirlConcertServiceImpl implements SuperGirlConcertService {
    public static final String SUPER_GIRL_CONCERT = "SUPER_GIRL_CONCERT";
    public static final String TOTAL_SUPER_GIRL_CONCERT = "TOTAL_SUPER_GIRL_CONCERT";

    @Resource
    SuperGirlConcertDao superGirlConcertDao;

    @Resource
    UserCacheService userCacheService;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    /**
     * 查询超女线下活动列表接口
     *
     * @return 返回活动列表
     */
    @Override
    public ResultDO<SuperGirlConcertDto> queryConcerts() {
        SuperGirlConcertDto superGirlConcertDto = new SuperGirlConcertDto();
        ResultDO<SuperGirlConcertDto> result = new ResultDO<SuperGirlConcertDto>();
        try {
            List<SuperGirlConcertActivityDto> validList = superGirlConcertDao.findConcertsValid();
            List<SuperGirlConcertActivityDto> unValidList = superGirlConcertDao.findConcertsUnValid();
            validList.addAll(unValidList);
            superGirlConcertDto.setSuperGirlConcertActivityDtos(validList);
            if (validList != null) {
                for (SuperGirlConcertActivityDto superGirlConcertActivityDto : validList) {
                    superGirlConcertActivityDto.setReservationNum(stringRedisTemplate.opsForSet().size(SUPER_GIRL_CONCERT + superGirlConcertActivityDto.getId()) == null ? "0" : stringRedisTemplate.opsForSet().size(SUPER_GIRL_CONCERT + superGirlConcertActivityDto.getId()).toString());

                }

            }
            //设置总预约人数
            superGirlConcertDto.setTotalReservation(stringRedisTemplate.opsForValue().get(TOTAL_SUPER_GIRL_CONCERT) == null ? "0" : stringRedisTemplate.opsForValue().get(TOTAL_SUPER_GIRL_CONCERT));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setResultCode(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_ARTIST_ERROR.getResultCode());
            result.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_ARTIST_ERROR.getMessage());
            result.setErrorMessage(e.getMessage());
        }
        result.setModule(superGirlConcertDto);
        return result;

    }


    /**
     * 根据活动id查询活动详情
     *
     * @param id 活动id
     * @return 活动详情
     */
    @Override
    public ResultDO<SuperGirlConcertDetailDto> queryConcertDetail(int id, String uid) {
        //查询活动基本信息
        ResultDO<SuperGirlConcertDetailDto> result = new ResultDO<SuperGirlConcertDetailDto>();
        try {
            SuperGirlConcertDetailDto superGirlConcertDetailDto = superGirlConcertDao.findActivityDetail(id);
            //判断下活动是否存在
            if (superGirlConcertDetailDto != null) {
                List<String> list = superGirlConcertDao.findStarsByActivity(id);
                if (list != null && list.size() >= 0) {
                    List<StarDto> artistList = new ArrayList<StarDto>();
                    ResultDO<List<User>> users = userCacheService.queryUserByUids(list);
                    for (User user : users.getModule()) {
                        StarDto starDto = new StarDto();
                        starDto.setUid(user.getUid());
                        starDto.setPhoto(PhotoUtil.formatPhoto_120h_120w_90q(user.getPhoto()));
                        starDto.setNickName(user.getNickName());
                        artistList.add(starDto);
                    }

                    //设置参与艺人列表
                    superGirlConcertDetailDto.setArtistList(artistList);

                    //设置预约标志
                    if (stringRedisTemplate.opsForSet().isMember(SUPER_GIRL_CONCERT + id, uid))
                        superGirlConcertDetailDto.setReservationFlag(1);
                    else
                        superGirlConcertDetailDto.setReservationFlag(0);


                    //设置预约人数
                    superGirlConcertDetailDto.setReservationNum(stringRedisTemplate.opsForSet().size(SUPER_GIRL_CONCERT + id) == null ? 0 : stringRedisTemplate.opsForSet().size(SUPER_GIRL_CONCERT + id));
                }
                //设置返回信息
                result.setModule(superGirlConcertDetailDto);

            }

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setResultCode(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_DETAIL_ERROR.getResultCode());
            result.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_DETAIL_ERROR.getMessage());
            result.setErrorMessage(e.getMessage());
        }
        return result;

    }


    /**
     * 报名参加活动接口
     *
     * @param id  活动id
     * @param uid 用户uid
     * @return
     */
    @Override
    public ResultDO<String> applyConceret(int id, String uid) {
        //活动预约
        ResultDO<String> result = new ResultDO<String>();
        try {
            SuperGirlConcertDetailDto superGirlConcertDetailDto = superGirlConcertDao.findActivityDetail(id);
            //判断下活动是否存在
            if (superGirlConcertDetailDto == null) {
                result.setSuccess(false);
                result.setResultCode(ResultCodeEnum.SUPER_GIRL_CONCERT_NO_ACTIVITY_ERROR.getResultCode());
                result.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CONCERT_NO_ACTIVITY_ERROR.getMessage());
            } else {
                ResultDO<User> user = userCacheService.queryUserByUid(uid);
                User u = user.getModule();
                //判断用户是否存在
                if (u != null) {
                    if (!stringRedisTemplate.opsForSet().isMember(SUPER_GIRL_CONCERT + id, uid)) {
                        stringRedisTemplate.opsForSet().add(SUPER_GIRL_CONCERT + id, uid);
                        stringRedisTemplate.opsForValue().increment(TOTAL_SUPER_GIRL_CONCERT, 1);
                        stringRedisTemplate.expire(SUPER_GIRL_CONCERT + id, 150, TimeUnit.DAYS);
                        stringRedisTemplate.expire(TOTAL_SUPER_GIRL_CONCERT, 150, TimeUnit.DAYS);
                    } else {
                        result.setSuccess(false);
                        result.setResultCode(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_JOIN_EXIST_ERROR.getResultCode());
                        result.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_JOIN_EXIST_ERROR.getMessage());
                    }
                } else {
                    result.setSuccess(false);
                    result.setResultCode(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_JOIN_NO_ID_ERROR.getResultCode());
                    result.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_JOIN_NO_ID_ERROR.getMessage());
                }
            }
            result.setModule(stringRedisTemplate.opsForSet().size(SUPER_GIRL_CONCERT + id) == null ? "0" : stringRedisTemplate.opsForSet().size(SUPER_GIRL_CONCERT + id).toString());

        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setResultCode(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_JOIN_ERROR.getResultCode());
            result.setErrorMessage(ResultCodeEnum.SUPER_GIRL_CONCERT_ACTIVITY_JOIN_ERROR.getMessage());
            result.setErrorMessage(e.getMessage());
        }
        return result;

    }
}
